package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.Map;

public class PjInfoDelete implements ServerInterface {
	
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			//获取入参
			@SuppressWarnings("unchecked")
			Map<String,String> map = JsonUtils.objectToPojo(req.getData(),Map.class);
			if(map==null||StringUtils.isEmpty(map.get("id"))) {
				return MessageBean.create(Constant.NOT_NULL, "单据id不能为空", null);
			}
			//获取单据信息
			PjInfoMapper mapper = factory.getMapper(PjInfoMapper.class);			
			PjInfoBean bean=mapper.selectById(map.get("id"));
			//非草稿状态不能删除
			if (!StringUtils.isEmpty(bean.getProcessState())) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "状态不是草稿,该单据不能删除", void.class);
			}
			// 删除
			mapper.delete(map.get("id"));
			return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
		} catch (Exception e) {
			logger.error("工程单据删除失败"+e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "删除失败", null);
		}
	}

}
